package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Person;
import com.constants.Constants;
import com.constants.UIMessages;

/**
 * Servlet implementation class ReloadAccountServlet
 */
public class ReloadAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReloadAccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			RequestDispatcher dispatcher;
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(Constants.sessionTimeOut);
			
			Person userData = (Person)session.getAttribute("PersonData");
			
			if(userData != null)
			{
				//Storing the input parameter
				request.setAttribute("firstName", userData.getFirstName());
				request.setAttribute("lastName", userData.getLastName());
				String address = userData.getAddress();
				String[] addresses = address.split("2:");
				request.setAttribute("address1", addresses[0]);
				if(addresses.length == 2)
				{
					request.setAttribute("address2", addresses[1]);
				}
				request.setAttribute("city", userData.getCity());
				request.setAttribute("state", userData.getState());
				request.setAttribute("zip", Integer.toString(userData.getZipCode()));
				request.setAttribute("email", userData.getEmailId());
				request.setAttribute("MembershipType", userData.getCustomerType());
				
				session.removeAttribute("ServerException");
				dispatcher = getServletContext().getRequestDispatcher("/editUserAccount.jsp");
				dispatcher.forward(request, response);
			}
			else
			{
				request.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
				session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
				dispatcher = getServletContext().getRequestDispatcher("/subPages.jsp");
				dispatcher.forward(request, response);
			}
			
		}
		catch(Exception e)
		{
			HttpSession session = request.getSession(true);
			request.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
			session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/subPages.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
